<template>
  <!-- 滚动条样式以及置顶功能 -->
  <div class="container">
    <header class="heade">标题</header>
    <div v-show="showTop" class="top" @click="backTop">置顶</div>
    <div class="con">
      <ul class="sc">
        <li>11111111</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
        <li>1</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showTop: false
    }
  },
  mounted() {
    window.addEventListener('scroll', this.jumpTop, true)
  },
  methods: {
    backTop() {
      document.getElementsByClassName('con')[0].scrollTop = 0
    },
    jumpTop() {
      const offsetTop = document.getElementsByClassName('con')[0].scrollTop
      console.log(offsetTop)
      if (offsetTop >= 100) {
        this.showTop = true
      } else {
        this.showTop = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  .heade {
    position: fixed;
    width: 70%;
    // background-color: black;
    height: 50px;
    color: white;
    font-size: 25px;
    text-align: center;
    line-height: 50px;
    left: 50%;
    transform: translatex(-50%);
    // z-index: 100000;
  }
  .con {
    flex: 1;
    overflow: scroll;
    overflow-x: hidden;
    position: relative;
    background-color: #fff;
    width: 100%;
    height: 500px;
    top: 50px;
    left: 50%;
    transform: translatex(-50%);
  }
  .con::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }
  .con::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 30px;
  }
  .con::-webkit-scrollbar-track {
    background: #fff;
  }
  .top {
    position: fixed;
    width: 40px;
    height: 40px;
    background-color: red;
    bottom: 50px;
    right: 50px;
    z-index: 999;
    text-align: center;
    line-height: 40px;
    cursor: pointer;
  }
}
</style>
